package com.brook.app.android.filepicker.ui;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.brook.app.android.filepicker.R;
import com.brook.app.android.filepicker.core.IThumbnailLoader;
import com.brook.app.android.filepicker.core.MimeType;
import com.brook.app.android.filepicker.util.DisplayUtil;

import java.io.File;
import java.util.List;

class FilePickerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<File> mCurrentPickerFileList;
    private final IThumbnailLoader mImageLoader;
    private final OnItemClickListener mOnItemClickListener;
    private final String mMimeType;
    private List<File> mFileList;

    FilePickerAdapter(@NonNull String mimeType, @NonNull List<File> files, @NonNull List<File> currentPickerFileList,
                      @NonNull IThumbnailLoader imageLoader, @NonNull OnItemClickListener listener) {
        this.mMimeType = mimeType;
        this.mFileList = files;
        this.mCurrentPickerFileList = currentPickerFileList;
        this.mImageLoader = imageLoader;
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        Context context = viewGroup.getContext();
        int itemWidth = (int) ((DisplayUtil.getScreenWidth(context) - DisplayUtil.dp2px(context, 1) * 2) / 3);
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.filepicker_item_camera, viewGroup, false);
            view.setLayoutParams(new ViewGroup.LayoutParams(itemWidth, itemWidth));
            return new CameraItemHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.filepicker_item_thumbnail, viewGroup, false);
            view.setLayoutParams(new ViewGroup.LayoutParams(itemWidth, itemWidth));
            view.setBackground(null);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int p) {
        int position = viewHolder.getAdapterPosition();
        int viewType = getItemViewType(position);
        if (viewType == 0) {
            CameraItemHolder cameraItemHolder = (CameraItemHolder) viewHolder;
            if (MimeType.VIDEO.toString().equals(mMimeType)) {
                cameraItemHolder.actionDesc.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.picker_ic_video, 0, 0);
                cameraItemHolder.actionDesc.setText("拍摄视频");
            } else {
                cameraItemHolder.actionDesc.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.picker_ic_camera, 0, 0);
                cameraItemHolder.actionDesc.setText("拍摄照片");
            }
            cameraItemHolder.mOnClickListener = mOnItemClickListener;
        } else {
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            File file = mFileList.get(position - 1);
            itemViewHolder.mTargetFile = file;
            itemViewHolder.mOnClickListener = mOnItemClickListener;

            itemViewHolder.ivSelectState.setSelected(mCurrentPickerFileList.contains(file));

            if (mImageLoader != null) {
                mImageLoader.loadThumbnail(file, itemViewHolder.ivThumbnail);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mFileList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    public void setFileList(List<File> files) {
        this.mFileList = files;
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public final ImageView ivThumbnail;
        public final ImageView ivSelectState;
        public File mTargetFile;
        public OnItemClickListener mOnClickListener;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ivThumbnail = itemView.findViewById(R.id.thumbnail);
            ivSelectState = itemView.findViewById(R.id.select_state);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnClickListener != null) {
                mOnClickListener.onItemClick(v, mTargetFile, getAdapterPosition());
            }
        }
    }

    private static class CameraItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public OnItemClickListener mOnClickListener;
        public TextView actionDesc;

        public CameraItemHolder(@NonNull View itemView) {
            super(itemView);
            actionDesc = itemView.findViewById(R.id.picker_action_desc);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnClickListener != null) {
                mOnClickListener.onItemClick(v, null, getAdapterPosition());
            }
        }
    }
}